package com.dawnClass.controller;

import com.dawnClass.pojo.Consumer;
import com.dawnClass.service.AllFollowsService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
@Controller
public class AllFollowsController {
    @Autowired
    private AllFollowsService allFollowsService;
    @GetMapping("allfollows")
    @ResponseBody
    public ModelAndView index(HttpSession session){
        //获取session中存储的consumer
        ServiceResponse consumerResponse = (ServiceResponse)session.getAttribute("ServiceResponse");
        Consumer consumer = (Consumer) consumerResponse.getdata();
        //通过用户id查询用户课程
        ServiceResponse serviceResponse2 = allFollowsService.getById(consumer.getConsumerId());

        ModelAndView mav = new ModelAndView();

        mav.addObject("serviceResponse2",serviceResponse2);

        mav.setViewName("user/allfollows");
        System.out.println("进入收藏页面");
        return mav;
    }
    @PostMapping("/allfollows")
    public String update(Integer collectionId){

        //修改收藏状态
         allFollowsService.updateaById(collectionId);
        System.out.println(" 取消collentionId：" + collectionId + " 收藏 ");
        //页面重定向到收藏页
        return  "redirect:/allfollows";
    }

}
